#include "CommandAction.h"
#include "GameboardView.h"
#include "MessagesView.h"

CommandAction::CommandAction(GameboardModel* m) {
	this->m = m;
}

void CommandAction::undoPlaceWall(PlayerModel* player, WallModel& wall) {
	player->incUnplacedWalls();
	m->gameboardView->switchColor(wall);
	Position* pos = wall.getPosition();
	m->placedWalls[pos->getX()][pos->getY()] = nullptr;
}

void CommandAction::doMovePawn(PlayerModel* player, Position& pos) {
	m->gameboardView->movePawn(m->gameboardView->getPatPawn(player), pos);
}

void CommandAction::doPlaceWall(PlayerModel* player, WallModel& wall) {
	m->gameboardView->createWall(player, wall);
}

void CommandAction::validateMovePawn(PlayerModel* player, Position& pos) {
	Position* pawnPos = player->getPawn();
	*pawnPos = pos;
}

void CommandAction::cancelPlaceWall(PlayerModel* player, WallModel& wall) {
	m->gameboardView->removeWall(player, wall);
}

void CommandAction::validatePlaceWall(PlayerModel* player, WallModel* wall) {
	Position* pos = wall->getPosition();
	m->placedWalls[pos->getX()][pos->getY()] = wall;
	m->gameboardView->switchColor(*wall);
	player->decUnplacedWalls();
}

bool CommandAction::checkPlaceWall(WallModel* wall) {
	Position* pos = wall->getPosition();
	m->placedWalls[pos->getX()][pos->getY()] = wall;
	if (!m->canReachOtherSide(m->player1) || !m->canReachOtherSide(m->player2)) {
		m->messagesView->displayMessage("- Impossible de placer le mur ici. Vous bloquez un joueur.");
		m->placedWalls[pos->getX()][pos->getY()] = nullptr;
		return false;
	}
	return true;
}